#include <sys/stat.h>
#include <stdio.h>
#include <string>
#include <vector>
#include <unistd.h>
#include <string.h>


#ifndef FILECREATOR_H
#define	FILECREATOR_H

#ifdef WIN32
#define DIRECTORY_SEPARATOR "\\"
#else
#define DIRECTORY_SEPARATOR "/"
#endif 

using namespace std;

class FileCreator
{
   
public:
    static FILE* getNewFile(const char* nameFile)
    {
         char* value = new char[strlen(nameFile)+1];
         strcpy(value,nameFile);
         vector<string> separated ;
         bool correctWords = splitStr(value,separated);    
        return correctWords ? createFile(separated): fopen("Logs.txt", "w+");
    }    
private:         
        static bool ifCharacterIsCorrect(const char* value)
        {
                char noCharacter[9] = {'<', '>', ':', '/', '|', '"', '?', '*' ,'\\'};
                bool acecpted = true;
                int index = 0;
                while(value!=0&&index<9&&(acecpted  = *value!=noCharacter[index++]));
                return acecpted;
        }

//It should not acept characters : < > : / | " ? \ * 
        static bool validateFileName(const char* word)
        {
                bool result = true;
                while(*word!='\0'&& (result= ifCharacterIsCorrect(word++) ))  ;
                return result;
        }
    
    static FILE* createFile(vector<string> separated)
    {
        
        char* name = (char*)"Logs.txt";
        if(separated.size()>0)
        {
            string absolutePath;
            int index = 0;
            while(separated[index].compare("..")==0&&(index++<separated.size())){ }   
             int i = 0;
             for( i = 0; i<index;i++)       absolutePath = absolutePath+separated[i]+"/";
            absolutePath = absolutePath+separated[index];
            if(index==0&&separated.size()!=1)
                absolutePath+= "/";
              name  = createDirectories((char*)absolutePath.c_str(),separated,index);
        }
        FILE* logFile = fopen(name, "w");
        return logFile;
    }
    
    static char* createDirectories( char* concatenated , vector<string> separated,int index)
    {
        if(index == separated.size()-1)
        {
            return concatenated;
        }
        else
        {
#ifdef WIN32
            ::CreateDirectory();
#else
            int desiredDirectoryFlags = S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH;
            int result = mkdir(concatenated,desiredDirectoryFlags);
#endif
            const char* nextDirectory ;
            nextDirectory = separated[++index].c_str();
            strcat(concatenated,nextDirectory);
            if(index<separated.size()-1)
                strcat(concatenated,"/");
            strcat(concatenated,"\0");
            char* real  = new char[strlen(concatenated)+1];
            strcpy(real,concatenated);
            return createDirectories(real,separated,index);
        }
    }
    
   static  bool  splitStr( char* name,vector<string>& vects)
    {
       char *result ;
       result = strtok(name,"/");
       bool correctWords = true;
       while((result!=NULL)&&correctWords)
       {

         correctWords = validateFileName(result);  
         vects.push_back(result);
         result = strtok(NULL,"/");
       }
       return correctWords;
    }
};

#endif	/* FILECREATOR_H */

